package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.vx.vo.WxBrandListRespVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WxBrandServiceImpl
 * @Author: WangEnzhao
 * @Description: 品牌模块
 * @CreateTime: 2021/12/20:15:00
 */
@Service
public class WxBrandServiceImpl implements WxBrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    /***
    * @Author: WangEnzhao
    * @Description: 查询brandList
    * @CreateTime: 20:30 12/2/2021
    * @Param: [baseParam]
    * @return: com.cskaoyan.model.vx.vo.WxBrandListRespVO
    */
    @Override
    public WxBrandListRespVO brandList(BaseParam baseParam) {
        //要返回的数据
        WxBrandListRespVO wxBrandListRespVO = new WxBrandListRespVO();
        if(baseParam == null || baseParam.getPage() == null || baseParam.getPage() == null){
            return wxBrandListRespVO;
        }

        //分页
        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        //将marketBrand转换类型
        ArrayList<WxBrandListRespVO.ListEntity> listEntities = new ArrayList<>();
        for (MarketBrand marketBrand : marketBrands) {
            WxBrandListRespVO.ListEntity listEntity = new WxBrandListRespVO.ListEntity();
            try {
                BeanUtils.copyProperties(listEntity,marketBrand);
                listEntities.add(listEntity);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        PageInfo pageInfo = new PageInfo(marketBrands);
        wxBrandListRespVO.setLimit(baseParam.getLimit());
        wxBrandListRespVO.setPage(baseParam.getPage());
        wxBrandListRespVO.setTotal((int)pageInfo.getTotal());
        wxBrandListRespVO.setPages((int)pageInfo.getTotal()/baseParam.getLimit() + 1);
        wxBrandListRespVO.setList(listEntities);
        return wxBrandListRespVO;
    }

    /***
    * @Author: WangEnzhao
    * @Description: 获取品牌详情
    * @CreateTime: 21:00 12/2/2021
    * @Param: [id]
    * @return: com.cskaoyan.bean.MarketBrand
    */
    @Override
    public MarketBrand brandDetail(Integer id) {
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }
}
